package com.scqkxx.gamebox.activity.space.adapter;

import android.content.ContentValues;
import android.util.Log;
import android.view.View;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.myself.MyselfEditSignatureActivity;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by admin on 2018/8/15. 09点37分
 * name: 唐师
 * 用途 ： 游戏页面的适配器
 */

public class RecyleAdapter extends BaseQuickAdapter<AVObject, BaseViewHolder> {

    private boolean isDeleteAble = false;

    public RecyleAdapter(int layoutResId, List<AVObject> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, AVObject item) {
        helper.setText(R.id.tv_space_name, item.getString("name"));
//        helper.setText( R.id.tv_space_content , item.getString("name"));
        helper.setText(R.id.tv_location, "[" + item.getString("spaceName") + "/" + item.get("positionName") + "]");
        helper.setText(R.id.tv_space_time, new SimpleDateFormat("yyyy年MM月dd日").format(item.getCreatedAt()) + " 建立");

        if (item.getInt("isPublic") == 0) {
            //秘密
            helper.setText(R.id.tv_miss, "设为公开");
        } else {
            helper.setText(R.id.tv_miss, "设为秘密");
        }
        helper.getView(R.id.btnMore).setVisibility(View.GONE);
        helper.getView(R.id.btnSecret).setVisibility(View.GONE);

//        //更多
//        helper.getView(R.id.btnMore).setOnClickListener(v -> {
//            ContentValues contentValues = new ContentValues();
//            contentValues.put("objectId", item.getObjectId());
//            contentValues.put("belongGroupId", (Integer) item.get("belongGroupId"));
//            contentValues.put("spaceId", (Integer) item.get("spaceId"));
//            ActivityUtil.switchTo(mContext, MyselfEditSignatureActivity.class, contentValues);
//        });
//        //设为秘密
//        helper.getView(R.id.btnSecret).setOnClickListener(v -> {
//            item.put("isPublic", item.getInt("isPublic") == 0 ? 1 : 0);
//            item.saveInBackground(new SaveCallback() {
//                @Override
//                public void done(AVException e) {
//                    if (e == null) {
//                        item.put("isPublic", item.getInt("isPublic"));
//                        notifyItemChanged(helper.getAdapterPosition());
//                    } else {
//                        Log.e("TAG", e.getMessage());
//                    }
//                }
//            });
//        });

        //设为删除
        helper.getView(R.id.btnDelete).setOnClickListener(v -> {
            item.put("isDisable", 2 );
            item.saveInBackground(new SaveCallback() {
                @Override
                public void done(AVException e) {
                    if (e == null) {
                        mData.remove(item);
                        notifyItemRemoved(helper.getAdapterPosition());
                        notifyItemChanged(helper.getAdapterPosition());
                    } else {
                        Log.e("TAG", e.getMessage());
                    }
                }
            });
        });

        helper.getView(R.id.contentView).setOnClickListener(v -> {
            ContentValues contentValues = new ContentValues();
            contentValues.put("objectId", item.getObjectId());
            contentValues.put("belongGroupId", (Integer) item.get("belongGroupId"));
            contentValues.put("spaceId", (Integer) item.get("spaceId"));
            ActivityUtil.switchTo(mContext, MyselfEditSignatureActivity.class, contentValues);
        });
    }
}
